﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PRO_DOCS.DocumentManagementSystem.ADMIN.WIN.Configurations.Documents.DocumentTypes
{
    public partial class MultipleSelectionsSetup : BLL.WIN.Templates.DevxConfigurationTemplate
    {
        private OPTIONAL_MULTIPLE_CHOICES_SELECTION _cURRENT_SELECTED_CHOICE;
        private OPTIONAL_MULTIPLE_CHOICES_SELECTION CURRENT_SELECTED_CHOICE
        {
            get { return _cURRENT_SELECTED_CHOICE; }
            set
            {
                _cURRENT_SELECTED_CHOICE = value;
                txtSelectionValue.Text = _cURRENT_SELECTED_CHOICE.SELECTION_NAME;
            }
        }
        private List<OPTIONAL_MULTIPLE_CHOICES_SELECTION> _cURRENT_CHOICES_LIST;

        public List<OPTIONAL_MULTIPLE_CHOICES_SELECTION> CURRENT_CHOICES_LIST
        {
            get
            {
                return _cURRENT_CHOICES_LIST;
            }
            set
            {
                _cURRENT_CHOICES_LIST = value;
            }
        }

        public MultipleSelectionsSetup(bool isMultipleChoices)
        {
            InitializeComponent();
            CURRENT_CHOICES_LIST = new List<OPTIONAL_MULTIPLE_CHOICES_SELECTION>();
            btnAddNew.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnAddNew_ItemClick);
            btnDeleteItem.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnDeleteItem_ItemClick);
            btnRefresh.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnRefresh_ItemClick);
            btnSaveUpdate.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnSaveUpdate_ItemClick);
            gcIsSelected.Visible = isMultipleChoices;
        }

        void btnSaveUpdate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SAVE_SELECTION();
        }

        private void SAVE_SELECTION()
        {
            if (!string.IsNullOrEmpty(txtSelectionValue.Text))
            {
                switch (CurrentRecordStatus)
                {
                    case RecordStatus.New:
                        {
                            CURRENT_SELECTED_CHOICE.IS_SELECTED = false;
                            CURRENT_SELECTED_CHOICE.SELECTION_NAME = txtSelectionValue.Text;
                            CURRENT_CHOICES_LIST.Add(CURRENT_SELECTED_CHOICE);
                            gridControl1.DataSource = CURRENT_CHOICES_LIST;
                            gridControl1.RefreshDataSource();
                            gridControl1.Refresh();
                            CLEAR_FORMS();
                        }
                        break;
                    case RecordStatus.Old:
                        {
                            gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gcSelectionName, txtSelectionValue.Text);
                        }
                        break;
                }
            }
            else
            {
                dxErrorProvider1.SetError(txtSelectionValue, BLL.WIN.Properties.ErrorProviderMessages.Default.InputLeftBlank.Replace("{name}", "Selection name"));
            }
        }

        void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RELOAD_SELECTIONS();
        }

        private void RELOAD_SELECTIONS()
        {
            CURRENT_CHOICES_LIST = CURRENT_CHOICES_LIST;
        }

        void btnDeleteItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.SelectedRowsCount > 0)
            {
                gridView1.DeleteSelectedRows();
                CLEAR_FORMS();
            }
        }

        private void CLEAR_FORMS()
        {
            CurrentRecordStatus = RecordStatus.New;
            CURRENT_SELECTED_CHOICE = new OPTIONAL_MULTIPLE_CHOICES_SELECTION();
        }

        void btnAddNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CLEAR_FORMS();
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gridView1.FocusedRowHandle > -1)
            {

                CURRENT_SELECTED_CHOICE = gridView1.GetFocusedRow() as OPTIONAL_MULTIPLE_CHOICES_SELECTION;

                CurrentRecordStatus = RecordStatus.Old;
            }
        }

        private void MultipleSelectionsSetup_Load(object sender, EventArgs e)
        {
            CLEAR_FORMS();
            gridControl1.DataSource = CURRENT_CHOICES_LIST;
        }

        private void txtSelectionValue_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                SAVE_SELECTION();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CURRENT_CHOICES_LIST = new List<OPTIONAL_MULTIPLE_CHOICES_SELECTION>();
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            CURRENT_CHOICES_LIST = new List<OPTIONAL_MULTIPLE_CHOICES_SELECTION>();

            for (int i = 0; i < gridView1.RowCount; i++)
            {
                CURRENT_CHOICES_LIST.Add(gridView1.GetRow(i) as OPTIONAL_MULTIPLE_CHOICES_SELECTION);
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}